#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2024/11/13 10:25
# @Author  : cfg
# @File    : install_multi_ui.py
# @Software: PyCharm
import os
import tkinter
from tkinter import font, filedialog, END
from tkinter.messagebox import showwarning

from ui.info_ui import InfoUI
from utils.jar_name_utils import get_version, get_artifact_id, get_group_id


class InstallMultiUI(tkinter.Frame):
    def __init__(self, window):
        super().__init__()
        self.window = window

        screen_width = self.window.winfo_screenwidth()
        screen_height = self.window.winfo_screenheight()
        self.window_width = 620
        self.window_height = 500
        self.window_x = (screen_width - self.window_width) / 2
        self.window_y = (screen_height - self.window_height) / 2

        self.font_YaHei_12 = font.Font(family="Microsoft YaHei", size=12)
        self.font_YaHei_10 = font.Font(family="Microsoft YaHei", size=10)

        self.place(x=0, y=0, width=self.window_width, height=self.window_height)

        self.label_file_path = tkinter.Label(self, text="文件夹路径：")
        self.label_file_path.place(x=80, y=40, width=100, height=30)
        self.label_file_path.config(font=self.font_YaHei_12)

        self.input_file_path = tkinter.Entry(self)
        self.input_file_path.place(x=180, y=40, width=320, height=30)
        self.input_file_path.config(font=self.font_YaHei_12)

        self.btn_choose = tkinter.Button(self, text="···", background="Ivory", command=self.on_btn_choose_click)
        self.btn_choose.place(x=510, y=42, width=25, height=25)
        self.btn_choose.config(font=self.font_YaHei_10, borderwidth=1)

        self.list_box = tkinter.Listbox(self)
        self.list_box.place(x=80, y=100, width=460, height=280)
        self.list_box.config(font=self.font_YaHei_10, borderwidth=0, selectborderwidth=0, )

        self.btn_yes = tkinter.Button(self, text=" 确  定 ", background='LightSeaGreen', command=self.on_btn_yes_click)
        self.btn_yes.place(x=220, y=400, width=75, height=38)
        self.btn_yes.config(font=self.font_YaHei_12)
        self.btn_yes.bind('<Enter>', self.on_btn_yes_hover)
        self.btn_yes.bind('<Leave>', self.on_btn_yes_no_hover)

        self.btn_exit = tkinter.Button(self, text=" 退  出 ", background='IndianRed', command=self.on_btn_exit_click)
        self.btn_exit.place(x=320, y=400, width=75, height=38)
        self.btn_exit.config(font=self.font_YaHei_12)
        self.btn_exit.bind('<Enter>', self.on_btn_exit_hover)
        self.btn_exit.bind('<Leave>', self.on_btn_exit_no_hover)

    def on_btn_choose_click(self):
        file_path = filedialog.askdirectory()
        self.input_file_path.delete('0', END)
        self.input_file_path.insert('0', file_path)

        file_list = os.listdir(file_path)
        for filename in file_list:
            file_abs_path = os.path.join(file_path, filename)
            if os.path.isfile(file_abs_path):
                self.list_box.insert(END, "  " + filename)

    def on_btn_yes_click(self):
        jar_folder_path = self.input_file_path.get()

        if not jar_folder_path:
            showwarning('警 告', '请输入jar包路径')
            return
        filenames = self.list_box.get('0', END)
        if not filenames:
            showwarning('警 告', '所选文件夹没有jar包')
            return

        cmd = ""
        for filename in filenames:
            filename = filename[2:]
            file_path = os.path.join(jar_folder_path, filename)
            group_id = get_group_id()
            artifact_id = get_artifact_id(filename)
            version = get_version(filename)
            cmd += 'mvn install:install-file -Dfile="{filepath}" -DgroupId={group_id} -DartifactId={artifact_id} -Dversion={version} -Dpackaging=jar&'.format(filepath=file_path,
                                                                                                                                                              group_id=group_id,
                                                                                                                                                              artifact_id=artifact_id,
                                                                                                                                                              version=version)
        cmd = cmd[:-1]
        info = InfoUI(cmd, self.window_width, self.window_height, self.window_x, self.window_y - 100)
        info.start()

    def on_btn_exit_click(self):
        self.quit()

    def on_btn_yes_hover(self, event):
        self.btn_yes.config(bg='DarkTurquoise')

    def on_btn_yes_no_hover(self, event):
        self.btn_yes.config(background='LightSeaGreen')

    def on_btn_exit_hover(self, event):
        self.btn_exit.config(bg='LightCoral')

    def on_btn_exit_no_hover(self, event):
        self.btn_exit.config(background='IndianRed')
